package com.airnewslinkosky.ticket.net;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public class SoapClientTest {
	
	private static SoapClientTest servicesOnClient;
	private Connection connection;
	private static final String TAG="SoapClient";
	private static final int EXCEPTIONHAPPENED = -1;
	
	private SoapClientTest(){
		connection = Connection.getInstance();
	}
	
	public static SoapClientTest getInstance(){
		if(servicesOnClient == null){
			servicesOnClient = new SoapClientTest();
		}
		return servicesOnClient;
	}
	
	public int login(String userName,String password){
		try {
    		String result = connection.invoke(true,"login",userName,password); 
    		Log.i("login", result);
    		return Integer.parseInt(result);
		} catch (Exception e) {
			Log.d("", e.getLocalizedMessage());
			return EXCEPTIONHAPPENED;
		}
	}
	
	public void register(String userName,String password,String mailAdress)throws IOException{
			String result;
			try {
				result = connection.invoke(true,"register", userName,password,mailAdress);
			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.d(TAG, "register XmlPullParserException");
			}
	}
	/**
	 * 
	 * @param currentVersion
	 * @return 
	 * 		the new apk url , if there has a new apk and the client need update
	 */
	public boolean needUpdateApk(String version){
		try {
			String result = connection.invoke(true,"needUpdateApk",version); 
    		return Boolean.parseBoolean((result));
		} catch (Exception e) {	
			e.printStackTrace();
			Log.d("", e.getMessage());
			return false;
		} 
	}
	/**
	 * 
	 * @param version
	 * @return
	 */
	public String getUpdateAPK(){
		
		try {
			String result = connection.invoke(true,"getUpdateAPK");
			return result;
		} catch (Exception e) {			
			Log.d("", e.getMessage());
			return "EXCEPTIONHAPPENED";
		} 
	}
	
	public byte[] getMainInfo(){
		try {
			String result = connection.invoke(false,"getMainInfo");
			return result.getBytes();
		} catch (Exception e) {			
//			Log.d("", e.getLocalizedMessage());
			e.printStackTrace();
			return null;
		} 
	}
}
